#include <pthread.h>
#include <unistd.h>
#include <iostream>

using std::cout;
using std::endl;

pthread_mutex_t mutex;
pthread_cond_t cond;

void * threadfunc(void * arg)
{
    sleep(3);

    //通知等待条件变量的线程
    pthread_cond_signal(&cond);

    return NULL;
}

int main()
{
    pthread_t pthid;
    pthread_create(&pthid, NULL, threadfunc, NULL);

    //动态初始化
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);
    cout << "111" << endl;

    pthread_cond_wait(&cond, &mutex);

    cout << "main thread exit" << endl;

    //等待子线程执行完毕
    pthread_join(pthid, NULL);
    return 0;
}

